#include "dazuoye.h"
#include "ui_dazuoye.h"
#include<qpainter>
dazuoye::dazuoye(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::dazuoye)
{
    ui->setupUi(this);
    timer=new QTimer();
    connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
    resize(550,700);
    QRectF rect(275,500,nodeWidth,nodeHeight);
    snake.append(rect);
    addTop();
    addTop();

    addNewReword();
    setWindowTitle("贪吃蛇源码小游戏");
}

dazuoye::~dazuoye()
{
    delete ui;
}

void dazuoye::keyPressEvent(QKeyEvent*event){
    switch(event->key()){
    case Qt::Key_Up:
        if(moveFlag!=DIR_DOWN){
        moveFlag=DIR_UP;
        }
        break;
    case Qt::Key_Down:
        if(moveFlag!=DIR_UP){
        moveFlag=DIR_DOWN;
        }
        break;
    case Qt::Key_Right:
        if(moveFlag!=DIR_LEFT){
        moveFlag=DIR_RIGHT;
        }
        break;
    case Qt::Key_Left:
        if(moveFlag!=DIR_RIGHT){
        moveFlag=DIR_LEFT;
        }
        break;
    case Qt::Key_Space:
        if(gameStart==false){
        gameStart=true;

        timer->start(time);
        }
        else{
        gameStart=false;
        timer->stop();
        }
        break;
    default:
        break;
    }
}

void dazuoye::timeout(){
    int count=1;
    if(snake[0].intersects(rewardNode)){
        count++;
        addNewReword();
    }

    while(count--){
    switch(moveFlag){
    case DIR_UP:
        addTop();
        break;
    case DIR_DOWN:
        addDown();
        break;
    case DIR_RIGHT:
        addLeft();
        break;
    case DIR_LEFT:
        addRight();
        break;
    default:
        break;

    }
    }
    deleteLast();
    update();

}
void dazuoye::addTop(){
    QPointF leftTop;
    QPointF rightBotom;
    if(snake[0].y()-nodeHeight<0)
    {
        leftTop=QPointF(snake[0].x(),this->height()-nodeHeight);
        rightBotom=QPointF(snake[0].x()+nodeWidth,this->height());
    }
    else{
    leftTop =QPointF(snake[0].x(),snake[0].y()-nodeHeight);
    rightBotom=snake[0].topRight();
    }
    snake.insert(0,QRectF());
}

void dazuoye::paintEvent(QPaintEvent*event){
    QPainter painter(this);
    QPen pen;
    QBrush brush;

    QPixmap pix;
    pix.load（"C:/Users/71472/Documents/Tencent Files/714725643/FileRecv/MobileFile/IMG_20221027_200725.jpg");
    painter.drawPixmap(0,0,550,700,pix);

    pen.setColor(Qt::black);
    brush.setColor(Qt::darkMagenta);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);

    for(int i=0;i<snake.length();i++)
    {
    painter.drawRect(snake[i]);
    }
    QWidget::paintEvent(event);

    pen.setColor(Qt::red);
    brush.setColor(Qt::red);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(rewardNode);

    if(checkContact()){
    QFont font("微软雅黑",30,QFont::ExtraLight,false);
    painter.setFont(font);
    painter.drawText((this->width()-300)/2,
    (this->height()-30)/2,
    QString("GAME OVER"));
    timer->stop();
    }
    QWidget::paintEvent(event);



}

void dazuoye::deleteLast(){
    snake.removeLast();
}

void dazuoye::addDown(){
    QPointF leftTop;
    QPointF rightBottom;

    if(snake[0].y()+nodeHeight*2>this->height()){
    leftTop=QPointF(snake[0].x(),0);
    rightBottom=QPointF(snake[0].x()+nodeWidth,nodeHeight);

    }
    else{
    leftTop=snake[0].bottomLeft();
    rightBottom=snake[0].bottomRight()+QPointF(0,nodeHeight);

    }
    snake.insert(0,QRectF(leftTop,rightBottom));



}

void dazuoye::addLeft(){
    QPointF leftTop;
    QPointF rightBottom;

    if(snake[0].x()-nodeWidth<0){
    leftTop=QPointF(this->width()-nodeWidth,snake[0].y());
    }
    else{
    leftTop=snake[0].topLeft()-QPointF(nodeWidth,0);
    }

    rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));
}

void dazuoye::addRight(){
    QPointF leftTop;
    QPointF rightBottom;
    if(snake[0].x()+nodeWidth*2>this->width()){
    leftTop=QPointF(0,snake[0].y());
    }
    else{
    leftTop=snake[0].topRight();
    }

    rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));

}

void dazuoye::addNewReword(){
    rewardNode =QRectF(
        rand()%(this->width()/30)*20,
        rand()%(this->height()/40)*20,
        nodeWidth,
        nodeHeight);

}

bool dazuoye::checkContact(){
    for(int i=0;i<snake.length();i++)
    for(int j=i+1;j<snake.length();j++)
    if(snake[i]==snake[j])
    {
        return true;
    }
    return false;
}
